package com.zl.protocol;

/**
 * Created by user on 2017-03-20.
 */
public class DateToHexStringUtil {

	public static void main(String[] args) {

		try {

			System.out.println(HexStringTodate("097C7DD1"));
			System.out.println(HexStringTodate("097C7E1C"));
			//String strDate = dateToHexString("2017-01-02 25:04:59");
			//System.out.println(strDate);
			//System.out.println(DateUtils.isDate(HexStringTodate("01D4EA25"),DateUtils.YYYY_MM_DD_HH_MM_SS));

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 *  返回：yyyy-MM-dd HH:mm:ss
	 */
	public static String HexStringTodate(String hexCurrentTime) {
		final StringBuilder stringBuilder = new StringBuilder();

		if (hexCurrentTime.isEmpty())
			return null;
		String BinaryString = hexString2binaryString(hexCurrentTime).toString();

		int cursor = 0;
		String temp = "";
		for (char chartemp : BinaryString.toCharArray()) {
			temp += chartemp;
			cursor++;
			// 年
			if (cursor == 6) {
				temp = String.valueOf(2017 + Integer.valueOf(temp, 2));
				stringBuilder.append(temp + "-");
				temp = "";
			}
			// 月
			else if (cursor == 10) {
				temp = Integer.valueOf(temp, 2).toString();
				if (1 == temp.length())
					temp = "0" + temp;

				stringBuilder.append(temp + "-");
				temp = "";
			}
			// 日
			else if (cursor == 15) {
				temp = Integer.valueOf(temp, 2).toString();
				if (1 == temp.length())
					temp = "0" + temp;
				stringBuilder.append(temp + " ");
				temp = "";
			}
			// 小时
			else if (cursor == 20) {
				temp = Integer.valueOf(temp, 2).toString();
				if (1 == temp.length())
					temp = "0" + temp;
				stringBuilder.append(temp + ":");
				temp = "";
			}
			// 分钟
			else if (cursor == 26) {
				temp = Integer.valueOf(temp, 2).toString();
				if (1 == temp.length())
					temp = "0" + temp;
				stringBuilder.append(temp + ":");
				temp = "";
			}
			// 秒
			else if (cursor == 32) {
				temp = Integer.valueOf(temp, 2).toString();
				if (1 == temp.length())
					temp = "0" + temp;
				stringBuilder.append(temp);
				temp = "";
			}

		}
		return stringBuilder.toString();
	}

	// 日期的格式必须是这个格式 yyyy-MM-dd HH:mm:ss
	/**
	 * 把日期转化成4字节的Hex 年的取值范围是：2017-2080
	 * 其中年占6位，月占4位，日占5位，时占5位，分占6位，秒占6位；也就是6+4+5+5+6+6=32位，四字节
	 */
	public static String dateToHexString(String currentTime) {
		final StringBuilder stringBuilder = new StringBuilder();

		if (currentTime.isEmpty()) {
			// return null;
		} else {
			String[] arrayDateTime;
			arrayDateTime = currentTime.split(" ");
			String[] arrayDate;
			String[] arrayTime;
			String year;
			String month;
			String day;
			String hour;
			String minute;
			String sen;

			if (arrayDateTime.length == 2) {
				arrayDate = arrayDateTime[0].split("-");
				arrayTime = arrayDateTime[1].split(":");
				if (arrayDate.length == 3 && arrayTime.length == 3) {
					year = arrayDate[0];
					month = arrayDate[1];
					day = arrayDate[2];
					hour = arrayTime[0];
					minute = arrayTime[1];
					sen = arrayTime[2];
					if (Integer.parseInt(year) - 2017 >= 0
							&& Integer.parseInt(year) - 2017 <= 63) {
						int year1 = Integer.parseInt(year) - 2017;
						year = Integer.toBinaryString(year1);
						year = addDigit(6, year);
						stringBuilder.append(year);
						month = Integer.toBinaryString(Integer.parseInt(month));
						month = addDigit(4, month);
						stringBuilder.append(month);
						day = Integer.toBinaryString(Integer.parseInt(day));
						day = addDigit(5, day);
						stringBuilder.append(day);
						hour = Integer.toBinaryString(Integer.parseInt(hour));
						hour = addDigit(5, hour);
						stringBuilder.append(hour);
						minute = Integer.toBinaryString(Integer
								.parseInt(minute));
						minute = addDigit(6, minute);
						stringBuilder.append(minute);
						sen = Integer.toBinaryString(Integer.parseInt(sen));
						sen = addDigit(6, sen);
						stringBuilder.append(sen);
					}
				}
			}
		}
		if (stringBuilder.toString().isEmpty()
				&& stringBuilder.toString().length() != 32) {
			return null;
		} else {
			return binaryString2hexString(stringBuilder.toString())
					.toUpperCase();
		}
	}

	/**
	 * 将二进制字符串换成十六进制字符串
	 * 
	 * @param bString
	 * @return
	 */
	public static String binaryString2hexString(String bString) {
		if (bString == null || bString.equals("") || bString.length() % 8 != 0)
			return null;
		StringBuffer tmp = new StringBuffer();
		int iTmp = 0;
		for (int i = 0; i < bString.length(); i += 4) {
			iTmp = 0;
			for (int j = 0; j < 4; j++) {
				iTmp += Integer.parseInt(bString.substring(i + j, i + j + 1)) << (4 - j - 1);
			}
			tmp.append(Integer.toHexString(iTmp));
		}
		return tmp.toString();
	}

	/**
	 * 将十六进制字符串换成二进制字符串
	 * 
	 * @param hexString
	 * @return
	 */
	public static String hexString2binaryString(String hexString) {
		if (hexString == null || hexString.length() % 2 != 0)
			return null;
		String bString = "", tmp;
		for (int i = 0; i < hexString.length(); i++) {
			tmp = "0000"
					+ Integer.toBinaryString(Integer.parseInt(
							hexString.substring(i, i + 1), 16));
			bString += tmp.substring(tmp.length() - 4);
		}
		return bString;
	}

	// 把年月日时分秒的位数补足
	private static String addDigit(int length, String str) {
		int curLength = str.length();
		if (str.length() < length) {
			for (int i = 0; i < length - curLength; i++) {
				str = "0" + str;
			}
		}
		return str;
	}
}
